package nc.impl.fa.copycard;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.bsl.fa.copycard.CopyCardBusiness;
import nc.impl.am.db.DBAccessUtil;
import nc.impl.fa.asset.AssetImpl;
import nc.itf.fa.copycard.ICopyCard;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ResultSetProcessor;
import nc.pub.fa.card.AssetCopyVariable;
import nc.vo.am.common.BizContext;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.fa.assetcard.CardVO;
import nc.vo.fa.copycard.CopyCardLog;
import nc.vo.org.AccountingBookVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDateTime;

public class CopyCardImpl implements ICopyCard{

	@Override
	public void doCopy() throws BusinessException {
		IUAPQueryBS QueryBS = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		List<DefdocVO> DefdocVOList = (List<DefdocVO>) QueryBS.retrieveByClause(DefdocVO.class, " nvl(dr,0) = 0 and enablestate =2 and pk_defdoclist in (select pk_defdoclist from bd_defdoclist where code ='BD033' and  nvl(dr,0) = 0 and enablestate =2)");
		for(DefdocVO vo:DefdocVOList){
			 
			AccountingBookVO accountBook = (AccountingBookVO) QueryBS.retrieveByPK(AccountingBookVO.class, vo.getDef1());
			
			
			StringBuffer sb = new StringBuffer();
			sb.append(" select distinct fa_card.pk_card   from fa_cardhistory fa_cardhistory ");
			sb.append("  inner join fa_card on fa_card.pk_card = fa_cardhistory.pk_card ");
			sb.append("  where (fa_cardhistory.laststate_flag = 'Y' or (fa_cardhistory.asset_state in ('exist') and fa_cardhistory.laststate_flag = 'N')) ");
			sb.append(" and (fa_card.pk_org = '"+accountBook.getPk_relorg()+"' AND  fa_cardhistory.pk_accbook = '"+accountBook.getPk_accountingbook()+"') ");
			sb.append(" and mod(fa_cardhistory.newasset_flag, 10) = 1 and fa_cardhistory.dr = 0  and fa_card.dr = 0 ");
			sb.append(" and fa_card.pk_card not in (select ORI_PK from fa_copycardlog where dr=0 and ORI_ACCOUNTBOOK='"+vo.getDef1()+"' and NEW_ACCOUNTBOOK = '"+vo.getDef2()+"')");
			BaseDAO dao = new BaseDAO(InvocationInfoProxy.getInstance().getUserDataSource());

			List<String> pks = (List<String>) dao.executeQuery(sb.toString(), new ResultSetProcessor() {
				private static final long serialVersionUID = 1L;
				List<String> pks = new ArrayList<String>();
				int i = 0;

				@Override
				public Object handleResultSet(ResultSet rs) throws SQLException {
					while (rs.next() && i++ < 10000) {
						pks.add(rs.getString(1));
					}
					return pks;
				}
			});
//			List<CardVO> cards = (List<CardVO>) QueryBS.retrieveByClause(CardVO.class,"pk_card not in (select ORI_PK from FA_COPYCARDLOG where ORI_ACCOUNTBOOK='"+vo.getDef1()+"' and NEW_ACCOUNTBOOK = '"+vo.getDef2()+"')");
			for(String pk_card:pks){
				try{
					NCLocator.getInstance().lookup(ICopyCard.class).copy__RequiresNew(pk_card,vo.getDef1(), vo.getDef2());
				}catch(BusinessException e){
					e.printStackTrace();
					System.out.println(e.getMessage());
				}				
			}
		}
	}

	@Override
	public void copy__RequiresNew(String pk_card,String pk_org_accountBook, String pk_accountBook) throws BusinessException {
		 CopyCardBusiness business = new CopyCardBusiness();
		 String newPK = business.copyCard(pk_card, pk_org_accountBook,pk_accountBook);
		 CopyCardLog logVO = new CopyCardLog();
		 logVO.setNew_accountbook(pk_accountBook);
		 logVO.setOri_accountbook(pk_org_accountBook);
		 logVO.setNew_pk(newPK);
		 logVO.setOri_pk(pk_card); 
		 new BaseDAO(InvocationInfoProxy.getInstance().getUserDataSource()).insertVO(logVO); 
	}

	
}
